from django.http import HttpResponse

from apps.api.models import models


# 数据初始化
def init(request):
    print("init")
    models.Department.objects.all().delete()
    models.Empolyee.objects.all().delete()
    addDepart(request)
    # addEmployee(request) 手动添加
    return HttpResponse("init")


def addDepart(request):
    print("addDepart")
    models.Department.objects.create(title="IT部门")
    models.Department.objects.create(title="销售部门")
    models.Department.objects.create(title="运营部门")
    return HttpResponse("addDepart")


def queryDepart(request):
    print("queryDepart")
    res = models.Department.objects.all()
    for obj in res:
        print(obj.id, obj.title)
    return HttpResponse("queryDepart")


def deleteDepart(request):
    print("deleteDepart")
    # 删除部门及关联的员工
    v1 = models.Department.objects.filter(id=3).delete()
    print(v1)
    return HttpResponse("deleteDepart")


def addEmployee(request):
    print("addEmployee")
    models.Empolyee.objects.create(name="张三", age=17, email="zhangsan@126.com", department_id=13)
    models.Empolyee.objects.create(name="李四", age=18, email="lisi@126.com", department_id=13)
    models.Empolyee.objects.create(name="王五", age=19, email="wangwu@126.com", department_id=14)
    models.Empolyee.objects.create(name="赵六", age=20, email="zhaoliu@126.com", department_id=14)
    models.Empolyee.objects.create(name="刘能", age=21, email="liuneng@126.com", department_id=15)
    models.Empolyee.objects.create(name="谢广坤", age=22, email="xieguangkun@126.com", department_id=15)
    return HttpResponse("addEmployee")


def addEmployeeByDepartement(request):
    obj = models.Department.objects.filter(id=13).first()
    models.Empolyee.objects.create(name="王大拿", age=17, email="wangdana@126.com", department=obj)
    models.Empolyee.objects.create(name='田娃', age=17, email="tianwa@126.com", department_id=obj.id)
    return HttpResponse("addEmployeeByDepartement")


def queryEmployeeWithDepart(request):
    print("queryEmployeeWithDepart")
    # select * from employee inner join api_departement ... 返回 queryset=[obj,obj,]
    print("\n select_related:select_related 通过多表 join 关联查询, 一次性获得所有数据, 只执行一次SQL查询 ")
    v1 = models.Empolyee.objects.filter(id__gt=13).select_related("department")
    print(v1)
    print(v1.query)
    for obj in v1:
        print(obj.id, obj.name, obj.age, obj.department.title)

    print("\n prefetch_related:分别查询每个表, 然后根据它们之间的关系进行处理, 执行两次查询 ")
    v1_1 = models.Empolyee.objects.filter(id__gt=13).prefetch_related("department")
    print(v1_1)
    print(v1_1.query)
    for obj in v1_1:
        print(obj.id, obj.name, obj.age, obj.department.title)

    # select * from employee inner join api_departement ...  返回 queryset=[{},{}]
    # v2 = models.Empolyee.objects.filter(id__gt=13).values("id", "name", "age", "department__title")
    # print(v2)
    # print(v2.query)
    # for obj in v2:
    #     print(obj["id"], obj["name"], obj["age"], obj["department__title"])

    # 返回列表元组  select * from employee inner join api_departement ...  queryset=[(),()]
    # v3 = models.Empolyee.objects.filter(id__gt=13).values_list("id", "name", "age", "department__title")
    # print(v3)
    # print(v3.query)
    # for obj in v3:
    #     print(obj[0], obj[1], obj[2], obj[3])
    return HttpResponse("queryEmployeeWithDepart")


def deleteEmployee(request):
    print("deleteEmployee")
    # 删除销售部的所有员工
    # obj = models.Department.objects.filter(title="销售部门").first()
    # models.Empolyee.objects.filter(department_id=obj.id).delete()

    models.Empolyee.objects.filter(department__title="运营部门", name='谢广坤').delete()
    return HttpResponse("deleteEmployee")
